package com.keke.app.controller;

import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.client.naming.NacosNamingService;
import com.keke.api.PrintServiceFacade;
import com.keke.api.UserServiceFacade;
import com.keke.api.vo.UserDTO;
import com.keke.api.vo.UserQueryReq;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.rpc.service.GenericService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/consumer")
@Slf4j
public class ConcumserContoller {

    @Autowired(required = false)
    private PrintServiceFacade printServiceFacade;

    @Autowired(required = false)
    private UserServiceFacade userServiceFacade;



    private static NamingService namingService;

    static {
        try {
            namingService = new NacosNamingService("localhost:8848");
            log.info(namingService.getServerStatus());
        }catch (Exception e){

        }
    }

    @GetMapping("/rpc")
    public String testDubboRpc(String param) throws Exception {
        List<Instance> instances = namingService.getAllInstances(param, false);
        for (Instance instance : instances) {
            Map<String, String> metaMap = instance.getMetadata();
            log.info("metaMap = {}",metaMap);
        }

        return printServiceFacade.sayHello(param);
    }


    @GetMapping("/getUser")
    public UserDTO getUser(String code) {

        UserDTO userDTO = userServiceFacade.getUserDtoByCode(code);
        return userDTO;
    }

    @PostMapping("/createUser")
    public Long createUser(@RequestBody UserDTO userDTO) {
        return userServiceFacade.createUser(userDTO);
    }

}
